<?php
namespace app\admin\controller;
use app\admin\logic\WrDataTargetMoneyLogic;

/**
 * 每日数据目标金额api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/21
 * Time: 14:44
 */
class WrDataTargetMoney extends Base {

    public function __construct(){
        parent::__construct();
        $this->wdtm_logic = new WrDataTargetMoneyLogic();
    }

    /**
     * 获取每日数据目标金额
     * @return \think\response\Json
     */
    public function ajax_get_wdtm(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->wdtm_logic->get_search_wstm_maps($params['department'],$params['year']);
        $limit = ($params['page'] - 1) * $params['rows'];
        $list = model('wr_data_target_money')->get_list($maps,'id,year,department,month_target_money','year desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $month_target_money = !empty($v['month_target_money']) ? json_decode($v['month_target_money'],true) : [];
            array_map(function ($value,$key) use (&$month_target_money){
                $month_target_money[$key] = deal_number_div_100($value);
            },$month_target_money,array_keys($month_target_money));

            $list[$k]['month_target_money'] = $month_target_money;
        }

        $total_num = model('wr_data_target_money')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->wdtm_logic->get_response(1,'OK',$data));
    }

    /**
     * 新增每日数据目标金额
     * @return \think\response\Json
     */
    public function ajax_add_wdtm(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查新增每日数据目标金额检查规则
        $r = $this->wdtm_logic->check_add_wdtm_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $wdtm_data = [];
        $wdtm_data['year'] = trim($params['year']);
        $wdtm_data['department'] = trim($params['department']);

        $month_target_money = $params['month_target_money'];
        array_map(function ($value,$key) use (&$month_target_money){
            $month_target_money[$key] = deal_number_mul_100($value);
        },$month_target_money,array_keys($month_target_money));
        $wdtm_data['month_target_money'] = json_encode($month_target_money,JSON_UNESCAPED_UNICODE);

        $r = model('wr_data_target_money')->insert($wdtm_data);
        if($r !== false){
            return json($this->wdtm_logic->get_response(1,'新增成功'));
        }
        return json($this->wdtm_logic->get_response(0,'新增失败'));
    }

    /**
     * 修改每日数据目标金额
     * @return \think\response\Json
     */
    public function ajax_save_wdtm(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查修改每日数据目标金额检查规则
        $r = $this->wdtm_logic->check_save_wdtm_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $wdtm_detail = $r['data']['wdtm_detail'];

        $wdtm_data = [];
        $wdtm_data['year'] = trim($params['year']);
        $wdtm_data['department'] = trim($params['department']);

        $month_target_money = $params['month_target_money'];
        array_map(function ($value,$key) use (&$month_target_money){
            $month_target_money[$key] = deal_number_mul_100($value);
        },$month_target_money,array_keys($month_target_money));
        $wdtm_data['month_target_money'] = json_encode($month_target_money,JSON_UNESCAPED_UNICODE);

        $maps = [];
        $maps[] = ['id','=',$wdtm_detail['id']];
        $r = model('wr_data_target_money')->force()->save($wdtm_data,$maps);
        if($r !== false){
            return json($this->wdtm_logic->get_response(1,'修改成功'));
        }
        return json($this->wdtm_logic->get_response(0,'修改失败'));
    }
}